package com.syztb_idea_gsf.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syztb_idea_gsf.entity.IconFile;
import com.syztb_idea_gsf.mapper.IconFileMapper;
import com.syztb_idea_gsf.service.IUserIconService;
import com.syztb_idea_gsf.utils.PathUtils;
import com.syztb_idea_gsf.utils.SystemConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.UUID;

@Slf4j
@Service
public class UserIconServiceImpl extends ServiceImpl<IconFileMapper, IconFile> implements IUserIconService {
    @Override
    public String saveIcon(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        IconFile iconFile = new IconFile();
        iconFile.setName(filename);

        String url = null;
        InputStream inputStream = file.getInputStream();
        String md5 = SecureUtil.md5(inputStream);
        IconFile iconFile1 = query().eq("md5", md5).one();

        if (iconFile1 != null) {
            url = iconFile1.getUrl();
        } else {
            // 本地文件处理
//            File newFile = new File(SystemConstants.fileUserIconPath);
//            if(!newFile.exists()) {
//                // 判断文件是否存在
//                // 不存在就创建
//                newFile.mkdir();
//            }
//            String newFilePath = newFile.getAbsolutePath() + "/";
//            String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
//            String finalFileName = uuid + ".jpg";
//            File targetFile = new File(newFilePath + finalFileName);
//            file.transferTo(targetFile);
//            url = "/src/main/resources/iconFile/" + finalFileName;
//            iconFile.setUrl(url);
//            iconFile.setMd5(md5);
//            this.baseMapper.insert(iconFile);


            // 线上文件处理
            ApplicationHome home = new ApplicationHome(getClass());
            File jarFile = home.getSource();
            if (jarFile != null) {
                // 获取 jar包 的文件路径
                String webPath = jarFile.getParentFile().getPath() + "/iconFile/";
                File newFile = new File(webPath);
                if (!newFile.exists()) {
                    newFile.mkdirs();
                }
                String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                String finalFileName = uuid + ".jpg";
                File targetFile = new File(webPath + finalFileName);
                file.transferTo(targetFile);
                url = webPath + finalFileName;
                iconFile.setUrl(url);
                iconFile.setMd5(md5);
                this.baseMapper.insert(iconFile);
            }


        }
        return url;
    }

}